'use strict';

const Controller = require('egg').Controller;
const Mock = require('mockjs');
const jwt = require('jsonwebtoken');

class HomeController extends Controller {
  async mock() {
    const { ctx, app } = this;

    // 模拟数据
    // const data = Mock.mock({
    //   'list|100-200': [
    //     {
    //       title: '@ctitle(3,6)',
    //       path: '@word(1,2)',
    //       'pv|5-100': 0,
    //       'status|1': [ '已发布', '待发布', '草稿', '已删除' ],
    //       date: '@date',
    //     },
    //   ],
    // });

    // // 将模拟好的数据添加到数据库中
    // data.list.forEach(async item => {
    //   await app.mysql.insert('lists', { title: item.title, path: item.path, pv: item.pv, status: item.status, date: item.date });
    // });

    const data = Mock.mock({
      'list|50-100': [
        {
          title: '@ctitle(5,10)',
          img: '@image(100x100, @color)',
          info: '@cword(20,30)',
          'classify|1': [ 'web', 'server', 'mobile', 'ai', 'bigdata', 'language' ],
          'classifytow|1': [ 'js', 'vue', 'react', 'webpack', 'node', 'egg', 'koa', 'express', 'h5', 'android', 'c', 'php', 'alpha', 'blockchain', 'eth' ],
        },
      ],
    });

    data.list.forEach(async item => {
      await app.mysql.insert('list', {
        ...item,
      });
    });


    ctx.body = {
      code: 1,
      msg: '添加成功',
    };

  }

  async getList() {
    const { ctx, app } = this;

    const { current, pageSize, title = '', path = '', status = '' } = ctx.request.body;

    // const data = await app.mysql.select('lists', {
    //   where: {
    //     path,
    //     status,
    //     title,
    //   },
    //   limit: pageSize, // 每页条数
    //   offset: (current - 1) * pageSize, // 起始数据的下标
    // });

    const data = await app.mysql.query(`
    select * from lists
    where title like "%${title}%"
    and path like "%${path}%"
    and status like "%${status}%"
    limit ${(current - 1) * pageSize}, ${pageSize}`);

    // 获取数据总条数
    const total = await app.mysql.query(`select count(*) as count from lists
    where title like "%${title}%"
    and path like "%${path}%"
    and status like "%${status}%"`);

    ctx.body = {
      code: 1,
      data,
      total: total[0].count,
    };
  }

  async deleteItem() {
    const { ctx, app } = this;

    await app.mysql.delete('lists', { id: ctx.request.query.id });

    ctx.body = {
      code: 1,
      msg: '删除成功',
    };
  }

  // 获取一级分类
  async getClassify() {
    const { ctx, app } = this;

    const data = await app.mysql.select('classify');

    ctx.body = {
      code: 1,
      data,
    };
  }

  // 获取二分类
  async getClassifyTow() {
    const { ctx, app } = this;

    const { type } = ctx.request.body;

    const data = await app.mysql.select('classifytow', {
      where: {
        type,
      },
    });

    ctx.body = {
      code: 1,
      data,
    };
  }

  // 获取列表
  async getCsdnList() {
    const { ctx, app } = this;

    const { classify = '', classifytow = '' } = ctx.request.body;

    const data = await app.mysql.query(`select * from list where classify like "%${classify}%" and classifytow like "%${classifytow}%"`);

    ctx.body = {
      code: 1,
      data,
    };
  }

  async ArticleType() {
    const { ctx, app } = this;

    // 获取一级类型以及二级类型
    let classify = await app.mysql.select('classify');

    // 获取二级类型
    const classifytow = await app.mysql.select('classifytow');

    classify = classify.map(item => {
      return {
        ...item,
        children: classifytow.filter(val => {
          return item.type === val.type;
        }),
      };
    });

    ctx.body = {
      code: 1,
      data: classify,
    };
  }


  // 发布文章
  async sendArticle() {
    const { ctx, app } = this;

    // 获取前端传来的参数
    const { title, content, classify, classifytow, info, img } = ctx.request.body;

    // 当前登录的用户信息
    const { authorization } = ctx.request.headers;

    const id = jwt.verify(authorization, '1908A');

    const userInfo = await app.mysql.get('userlist', { id });

    // 新建文章
    await app.mysql.insert('list', {
      title,
      content,
      classify,
      classifytow,
      info,
      img,
      // 用户
      username: userInfo.name,
      userid: userInfo.id,
      userimg: userInfo.img,
    });

    ctx.body = {
      code: 1,
      msg: '发布成功',
    };
  }

  // 详情
  async getDetail() {
    const { ctx, app } = this;

    const { id } = ctx.request.body;

    const data = await app.mysql.get('list', { id });

    ctx.body = {
      code: 1,
      data,
    };
  }
}

module.exports = HomeController;
